<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Coupon_entity{
	var $id = 0;
	var $code = "";
	var $validity = "";
	var $coin = 0;
	var $user_redeem = 0;
	var $start_date = 0;
	var $end_date = 0;
	var $create_date = 0;
	var $mode = "";//single|multiple
	var $is_auction = 'no';
	var $username = '';
	var $credits = '';
	
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_COUPON)->result();
		}
		else if($cmd == 'code')
		{
			$result = $ci->db->where('code',$input)->get(TBL_COUPON)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->code = $result[0]->code;
			$this->validity = $result[0]->validity;
			$this->coin = $result[0]->coin;
			$this->user_redeem = $result[0]->user_redeem;
			$this->start_date = $result[0]->start_date;
			$this->end_date = $result[0]->end_date;
			$this->create_date = $result[0]->create_date;
			$this->mode = $result[0]->mode;
			$this->is_auction = $result[0]->is_auction;
			$this->username = $result[0]->username;
			$this->credits = $result[0]->credits;
			
			return $this;
		}
		return false;
	}
	
	function setCode($input){ $this->code=$input; }
	function setValidity($input){ $this->validity=$input; }
	function setCoin($input){ $this->coin=$input; }
	function setUserRedeem($input){ $this->user_redeem=$input; }
	function setStartDate($input){ $this->start_date=$input; }
	function setEndDate($input){ $this->end_date=$input; }
	function setCreateDate($input){ $this->create_date=$input; }
	function setMode($input){ $this->mode=$input; }
	function setIsAuction($input){ $this->is_auction=$input;}
	function setUsername($input){ $this->username=$input; }
	function setCredits($input){ $this->credits=$input; }
	
	function getId(){ return $this->id; }
	function getCode(){ return $this->code; }
	function getValidity(){ return $this->validity; }
	function getCoin(){ return $this->coin; }
	function getUserRedeem(){ return $this->user_redeem; }
	function getStartDate(){ return $this->start_date; }
	function getEndDate(){ return $this->end_date; }
	function getCreateDate(){ return $this->create_date; }
	function getMode(){ return $this->mode; }
	function getIsAuction(){ return $this->is_auction;}
	function getUsername(){ return $this->username; }
	function getCredits(){ return $this->credits; }
	
	function update()
	{
		$ci=& get_instance();
		 
		$ci->db->set('code', $this->code);
		$ci->db->set('validity', $this->validity);
		$ci->db->set('coin', $this->coin);
		$ci->db->set('user_redeem', $this->user_redeem);
		$ci->db->set('start_date', $this->start_date);
		$ci->db->set('end_date', $this->end_date);
		$ci->db->set('create_date', $this->create_date);
		$ci->db->set('mode', $this->mode);
		$ci->db->set('is_auction', $this->is_auction);
		$ci->db->set('username', $this->username);
		$ci->db->set('credits', $this->credits);
		
		$ci->db->where('id', $this->id)->update(TBL_COUPON);
	}
	
	function createNewEntity(){
		$ci=& get_instance();
		
		$ci->db->set('user_redeem', 0);
		$ci->db->insert(TBL_COUPON);
		
		return $ci->db->insert_id();
	}
	
}
